import pygame
from pygame.locals import *
import socket
import time

#command window
black = (0,0,0)
white = (255,255,255)

pygame.init()
size=width,height=640,391;
screen=pygame.display.set_mode(size);
pygame.display.set_caption("plpbot control window")
pygame.mouse.set_visible(1)
pygame.key.set_repeat(60,0)
not_exit = True 

TCP_IP = socket.gethostbyname(socket.gethostname()) 
TCP_PORT = 1337
BUFFER_SIZE = 1024

s = socket.create_connection((TCP_IP, TCP_PORT))

plp = pygame.image.load("plpbot.png").convert()
up = pygame.image.load("up.png").convert()
down = pygame.image.load("down.png").convert()
left = pygame.image.load("left.png").convert()
right = pygame.image.load("right.png").convert()
first_gear = pygame.image.load("firstgear.png").convert()
second_gear = pygame.image.load("secondgear.png").convert()
third_gear = pygame.image.load("thirdgear.png").convert()
reverse_gear = pygame.image.load("reversegear.png").convert()

gear = 1
left_motor = chr(64)
right_motor = chr(192) 
header = "#"
clock=pygame.time.Clock()

while not_exit:
	
	
	for event in pygame.event.get():
	
		screen.blit(plp,[0,0])
	        clock.tick(20)
		pygame.event.pump()

		if event.type == KEYDOWN and event.key == K_ESCAPE:
			not_exit = False
			
		elif event.type == KEYDOWN and event.key == K_1:
			gear = 1
			print "1"
			screen.blit(first_gear,[541,193])               
	
		elif event.type == KEYDOWN and event.key == K_2:
			gear = 2
			print "2"
			screen.blit(second_gear,[541,119])
	
		elif event.type == KEYDOWN and event.key == K_3:
			gear = 3
			print "3"
			screen.blit(third_gear,[541,45])

		#Jacob ruined our fun and didn't program for 4 gears
		#	 elif event.type == KEYDOWN and event.key == K_4:
		#	gear = 4
		#	print "4"""
	
		elif event.type == KEYDOWN and event.key == K_r:
			gear = 5
			print "R"
			screen.blit(reverse_gear,[541, 267])
	

                elif event.type == KEYDOWN and gear == 1 and event.key == K_UP and event.key == K_LEFT:
                        print "Soft left in gear 3"
                        left_motor = "095"
                        right_motor = "230"
                        screen.blit(right,[311,139])

	
                elif event.type == KEYDOWN and gear == 1 and event.key == K_UP and event.key == K_RIGHT:
                        print "Soft right in gear 1"
                        left_motor = "080"
                        right_motor = "203"
                        screen.blit(right,[311,139])

                elif event.type == KEYDOWN and gear == 1 and event.key == K_UP and event.key == K_LEFT:
                        print "Soft left in gear 1"
                        left_motor = "072"
                        right_motor = "208 "
                        screen.blit(right,[311,139])

                elif event.type == KEYDOWN and gear == 2 and event.key == K_UP and event.key == K_RIGHT:
                        print "Soft right in gear 2"
                        left_motor = "095"
                        right_motor = "213"
                        screen.blit(right,[311,139])

                elif event.type == KEYDOWN and gear == 1 and event.key == K_UP and event.key == K_LEFT:
                        print "Soft left in gear 2"
                        left_motor = "085"
                        right_motor = "208"
                        screen.blit(right,[311,139])

                elif event.type == KEYDOWN and gear == 3 and event.key == K_UP and event.key == K_RIGHT:
                        print "Soft right in gear 3"
                        left_motor = "110"
                        right_motor = "225"
                        screen.blit(right,[311,139])

                elif event.type == KEYDOWN and gear == 1 and event.key == K_UP and event.key == K_LEFT:
                        print "Soft left in gear 3"
                        left_motor = "095"
                        right_motor = "230"
                        screen.blit(right,[311,139])

	
		elif event.type == KEYDOWN and gear == 1 and event.key == K_UP:
				print "Forward in gear 1"
				left_motor = chr(120)
				right_motor = chr(248)
				screen.blit(up,[234,62])
			
		elif event.type == KEYDOWN and gear == 2 and event.key == K_UP:
			print "Forward in gear 2"
			left_motor = chr(95)
			right_motor = chr(223)
			screen.blit(up,[234,62])
			
		elif event.type == KEYDOWN and gear == 3 and event.key == K_UP:
			print "Forward in gear 3"
			left_motor = chr(126)
			right_motor = chr(254)
			screen.blit(up,[234,62])
			
		#elif event.type == KEYDOWN and gear == 4 and event.key == K_UP:
		#	print "Forward in gear 4"
		#	left_motor = "125"
		#	right_motor = "253"""
			
		elif event.type == KEYDOWN and gear == 5 and event.key == K_DOWN:
			print "Go in reverse"
			left_motor = chr(1)
			right_motor = chr(128)
			screen.blit(down,[234,216])
			
		elif event.type == KEYDOWN and gear == 5 and event.key == K_LEFT:
			print "Go in left in reverse"
			left_motor = chr(1)
			right_motor = chr(192)
			screen.blit(left,[157,139])
	                screen.blit(down,[234,216])

		elif event.type == KEYDOWN and gear == 5 and event.key == K_RIGHT:
			print "Go in right in reverse"
			left_motor = chr(64)
			right_motor = chr(128)
			screen.blit(right,[311,139])
        	        screen.blit(down,[234,216])
	
		elif event.type == KEYDOWN and gear == 1 and event.key == K_LEFT:
			print "Left in gear 1"
			left_motor = chr(64)
			right_motor = chr(208)
			screen.blit(left,[157,139])
			
		elif event.type == KEYDOWN and gear == 2 and event.key == K_LEFT:
			print "Left in gear 2"
			left_motor = chr(64)
			right_motor = chr(223)
			screen.blit(left,[157,139])
			
		elif event.type == KEYDOWN and gear == 3 and event.key == K_LEFT:
			print "Left in gear 3"
			left_motor = chr(64)
			right_motor = chr(238)
			screen.blit(left,[157,139])
			
		#elif event.type == KEYDOWN and gear == 4 and event.key == K_LEFT:
		#	print "Left in gear 4"
		#	left_motor = "064"
		#	right_motor = "253"""
			
		elif event.type == KEYDOWN and gear == 1 and event.key == K_RIGHT:
			print "Right in gear 1"
			left_motor = chr(80) 
			right_motor = chr(192)
			screen.blit(right,[311,139])
			
		elif event.type == KEYDOWN and gear == 2 and event.key == K_RIGHT:
			print "Right in gear 2"
			left_motor = chr(95)
			right_motor = chr(192)
			screen.blit(right,[311,139])
			
		elif event.type == KEYDOWN and gear == 3 and event.key == K_RIGHT:
			print "Right in gear 3"
			left_motor = chr(110)
			right_motor = chr(192)
			screen.blit(right,[311,139])
			
		#elif event.type == KEYDOWN and gear == 4 and event.key == K_RIGHT:
		#	print "Right in gear 4"
		#	left_motor = "125"
		#	right_motor = "192"""

           	else:
			left_motor = chr(64)
			right_motor = chr(192)

		if gear == 1:
			screen.blit(first_gear,[541,193])
		elif gear == 2:
			screen.blit(second_gear,[541,119])
		elif gear == 3:
			screen.blit(third_gear,[541,45])
		elif gear == 5:
			screen.blit(reverse_gear,[541,267])
		
	s.send(header)
	s.send(left_motor)
	s.send( right_motor)
	time.sleep(.01)
	font = pygame.font.Font(None, 25)
	pygame.display.flip()
					
pygame.quit()
exit(0)
